package com.health.ts.controller.cost;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.ApiResult;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.cost.CostInfo;
import com.health.ts.entity.cost.CostList;
import com.health.ts.service.cost.CostDetailsService;
import com.health.ts.service.cost.CostInfoService;
import com.health.ts.service.cost.CostListService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;
import com.health.ts.utils.SessionUserUtil;
/** 
 * 费用管理
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/api/cost",produces= BaseController.JSON_UTF8)
public class CostController extends BaseController {
	
	@Autowired
	CostInfoService costInfoService;
	
	@Autowired
	CostDetailsService costDetailsService;
	
	@Autowired
	CostListService  costListService;
	
	/**  
	* @Title: addCost  
	* @Description: 新增费用项目
	* @param @param costInfo
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ApiResult addCost(@RequestBody CostInfo costInfo) {
		QueryWrapper<CostInfo> wrapper = new QueryWrapper<CostInfo>();
		wrapper.eq("doc_id", costInfo.getDoc_id());
		wrapper.eq("department_id", costInfo.getDepartment_id());
		wrapper.eq("hospital_id", costInfo.getHospital_id());
		CostInfo info = costInfoService.getOne(wrapper);
		AssertUtil.isNull(info, "此医生费用记录已存在，不能重复添加");
		costInfo.setId(GenerateIdUtil.makeID());
		costInfo.setCreate_time(new Date());
		costInfoService.save(costInfo);
		return ApiResult.success();
	}
	
	/**  
	* @Title: updateCost  
	* @Description: 修改费用项目
	* @param @param cost_id
	* @param @param costInfo
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/update/{id}", method=RequestMethod.PUT)
	public ApiResult updateCost(@PathVariable String id,@RequestBody CostInfo costInfo) {
		CostInfo info = costInfoService.getById(id);
		AssertUtil.notNull(info, "要修改的费用不存在！");
		costInfo.setId(id);
		costInfo.setUpdate_time(new Date());
		costInfoService.updateById(costInfo);
		return ApiResult.success();
	}
	
	/**  
	* @Title: deleteCost  
	* @Description: 删除费用项目
	* @param @param cost_id
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE)
	public ApiResult deleteCost(@PathVariable String id) {
		costInfoService.removeById(id);
		return ApiResult.success();
	}
	
	@RequestMapping(value="/select/{costId}", method=RequestMethod.GET)
	public ApiResult selectCost(@PathVariable String costId) {
		CostInfo costInfo=costInfoService.getById(costId);
		AssertUtil.notNull(costInfo, "要查询的费用不存在！");
		return ApiResult.success();
	}
	
	/**
	 * 根据医生ID查询医生费用信息
	 * @param docId
	 * @return
	 */
	@RequestMapping(value="/selectByDoctor/{docId}", method=RequestMethod.GET)
	public ApiResult selectCostByDoctor(@PathVariable String docId) {
		QueryWrapper<CostInfo> costWrapper = new QueryWrapper<CostInfo>();
		costWrapper.eq("doc_id", docId);
		List<CostInfo> result = costInfoService.list(costWrapper);
		AssertUtil.notEmpty(result, "没有找到医生的费用信息");
		return ApiResult.success(result.get(0));
	}
	
	/**  
	* @Title: listCostPager  
	* @Description: 查询费用项目 查询视图
	* @param @param page
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
//	@RequiresRoles("role_cost")
//	@RequiresPermissions("authority_cost")
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public ApiResult listCostPager(FrontPage page) {
//		boolean hasRole = SessionUserUtil.hasRole("role_cost");
//		boolean permitted = SessionUserUtil.isPermitted("authority_cost2");
		QueryWrapper<CostList> wrapper = new QueryWrapper<CostList>();
		String user_name =getParam(page,"user_name");
		if (StringUtils.isNotEmpty(user_name)){
			wrapper.like("user_name", user_name); 
		}
		String hospital_name =getParam(page,"hospital_name");
		if (StringUtils.isNotEmpty(hospital_name)){
			wrapper.like("hospital_name", hospital_name); 
		}
		String position_code =getParam(page,"position_code");
		if (StringUtils.isNotEmpty(position_code)&&!"1".equals(position_code)){
			wrapper.eq("position_code", position_code); 
		}
		String department_name =getParam(page,"department_name");
		if (StringUtils.isNotEmpty(department_name)){
			wrapper.like("department_name", department_name); 
		}
		wrapper.orderByDesc("create_time");
		Page<CostList> pageList = costListService.page(page.getPagePlus(), wrapper);
		CustomPage<CostList> customPage = new CustomPage<CostList>(pageList);
		return ApiResult.success(customPage);
	}
}
